package br.ufrj.ppgi.jemf_advanced_example.volunteer;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Volunteer;
import br.ufrj.ppgi.jemf.mobile.view.VolunteerView;
import br.ufrj.ppgi.jemf.utils.CustomConversions;
import br.ufrj.ppgi.jemf.utils.CustomFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Volunteer detail screen.
 * This fragment is either contained in a {@link VolunteerListActivity} in two-pane mode (on tablets) or a {@link VolunteerDetailActivity} on handsets.
 */
public class VolunteerDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_VOLUNTEER = "activated_volunteer";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "VOLUNTEER";
    /**
     * The Volunteer content this fragment is showing.
     */
    private static Volunteer volunteer;
    /**
     * The Volunteer view holder.
     */
    private static VolunteerView volunteerView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public VolunteerDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Volunteer content specified by the fragment arguments.
        if (getArguments().containsKey(VolunteerDetailFragment.ARG_ID)) {
        	volunteer = getArguments().getParcelable(VolunteerDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Volunteer content
	    if (volunteer != null) {
	    	volunteerView = new VolunteerView(getActivity(), false);
	    	// Name
	    	volunteerView.setVolunteerNameText(volunteer.getName());
	    	// Gender
	    	volunteerView.setVolunteerGenderBool(CustomFormat.IntToBool(volunteer.getGender()));
	    	// Birth Date
	    	volunteerView.setVolunteerBirthDateText(volunteer.getBirthDateString());
	    	// Age
	    	volunteerView.setVolunteerAgeText(String.valueOf(volunteer.getAge()));
	    	// Login
	    	volunteerView.setVolunteerLoginText(volunteer.getLogin());
	    	// Password
	    	volunteerView.setVolunteerPasswordText(volunteer.getPassword());

	    	// Its necessary to add the VolunteerView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Volunteer View as a child of the ScrollView.
	    	scrollView.addView(volunteerView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Volunteer
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_VOLUNTEER)) {
        	volunteer = savedInstanceState.getParcelable(STATE_VOLUNTEER);
        }
	}





	/**
     * Get updated Volunteer
     * @return Volunteer
     */
	public static Volunteer getVolunteer() {
		if (BuildConfig.DEBUG) Log.i(VolunteerListActivity.TAG, "VolunteerDetailFragment ::: getVolunteer");
    	return volunteer;
    }

	/**
	 * Update Volunteer data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((volunteer != null) && (volunteerView != null)) {
    		if (BuildConfig.DEBUG) Log.i(VolunteerListActivity.TAG, "VolunteerDetailFragment ::: updateDataFromUI");
    		// Update Volunteer object with new data included by user through components
    		volunteer.setName(volunteerView.getVolunteerNameText());
    		volunteer.setGender(CustomFormat.BoolToInt(volunteerView.getVolunteerGenderBool()));
    		volunteer.setBirthDateString(volunteerView.getVolunteerBirthDateText());
    		volunteer.setAge(Integer.valueOf(volunteerView.getVolunteerAgeText()));
    		if (volunteerView.getVolunteerAgeText().isEmpty()) {
    			volunteer.setAge(0);
    		} else {
    			volunteer.setAge(CustomConversions.birthDateToAge(volunteer.getBirthDate()));
    	    	// Updating Age TO UI.
        		volunteerView.setVolunteerAgeText(String.valueOf(volunteer.getAge()));
    		}
    		volunteer.setLogin(volunteerView.getVolunteerLoginText());
    		volunteer.setPassword(volunteerView.getVolunteerPasswordText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Volunteer.
        outState.putParcelable(STATE_VOLUNTEER, volunteer);
    }
}
